package com.itbuka.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.itbuka.goods.domain.GoodsBrand;
import com.itbuka.goods.mapper.GoodsBrandMapper;
import com.itbuka.goods.service.GoodsBrandService;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import java.util.List;
import java.util.Map;

/**
 * @author ld
 * @description 针对表【goods_brand(品牌表)】的数据库操作Service实现
 * @createDate 2024-07-27 15:57:01
 */
@Service
public class GoodsBrandServiceImpl implements GoodsBrandService {

    @Autowired
    private GoodsBrandMapper goodsBrandMapper;

    @Override
    public List<GoodsBrand> selectAll() {
        LambdaQueryWrapper<GoodsBrand> lqw = new LambdaQueryWrapper<>();
        lqw.eq(GoodsBrand::getIsDelete, 0);
        return goodsBrandMapper.selectList(lqw);
    }

    @Override
    public List<GoodsBrand> selectList(GoodsBrand iGoodsBrand) {
        LambdaQueryWrapper<GoodsBrand> lqw = this.lqw(iGoodsBrand);
        return goodsBrandMapper.selectList(lqw);
    }

    @Override
    public Integer insert(GoodsBrand iGoodsBrand) {
        return goodsBrandMapper.insert(iGoodsBrand);
    }

    @Override
    @Transactional
    public Integer delete(String ids) {
        try {
            String[] split = ids.split(",");
            for (String id : split) {
                GoodsBrand iGoodsBrand = new GoodsBrand();
                iGoodsBrand.setId(Long.parseLong(id));
                iGoodsBrand.setIsDelete(1);
                goodsBrandMapper.updateById(iGoodsBrand);
            }
            return 1;
        } catch (Exception e) {
            return -1;
        }
    }

    @Override
    public Integer update(GoodsBrand iGoodsBrand) {
        return goodsBrandMapper.updateById(iGoodsBrand);
    }

    @Override
    public Page<GoodsBrand> pageAll(Integer page, Integer size) {
        PageHelper.startPage(page, size);
        return (Page<GoodsBrand>) this.selectAll();
    }

    @Override
    public Page<GoodsBrand> pageList(GoodsBrand iGoodsBrand, Integer page, Integer size) {
        PageHelper.startPage(page, size);
        return (Page<GoodsBrand>) this.selectList(iGoodsBrand);
    }


    /**
     * 品牌启用、停用 (status非null ，前端判断，null 后端判断)
     */
    @Override
    public Integer status(Long id, Integer status) {
        GoodsBrand brand = new GoodsBrand();
        brand.setId(id);
        brand.setStatus(status);
        return goodsBrandMapper.updateById(brand);
    }

    @Override
    public Integer status(Long id) {
        GoodsBrand brand = goodsBrandMapper.selectById(id);
        if (brand == null) {
            return -1;
        }
        if (brand.getStatus() == 1) {
            brand.setStatus(0);
        } else {
            brand.setStatus(1);
        }
        return goodsBrandMapper.updateById(brand);
    }

    @Override
    public List<Map<?,?>> findListByCategoryName(String categoryName) {
        return goodsBrandMapper.findListByCategoryName(categoryName);
    }



    public LambdaQueryWrapper<GoodsBrand> lqw(GoodsBrand iGoodsBrand) {
        LambdaQueryWrapper<GoodsBrand> lqw = new LambdaQueryWrapper<>();
        if (iGoodsBrand.getId() != null) {
            lqw.eq(GoodsBrand::getId, iGoodsBrand.getId());
        }
        if (iGoodsBrand.getBrandName() != null) {
            lqw.eq(GoodsBrand::getBrandName, iGoodsBrand.getBrandName());
        }
        if (iGoodsBrand.getBrandLogo() != null) {
            lqw.eq(GoodsBrand::getBrandLogo, iGoodsBrand.getBrandLogo());
        }
        if (iGoodsBrand.getStatus() != null) {
            lqw.eq(GoodsBrand::getStatus, iGoodsBrand.getStatus());
        }
        if (iGoodsBrand.getIsDelete() != null) {
            lqw.eq(GoodsBrand::getIsDelete, iGoodsBrand.getIsDelete());
        }
        if (iGoodsBrand.getCreateTime() != null) {
            lqw.eq(GoodsBrand::getCreateTime, iGoodsBrand.getCreateTime());
        }
        if (iGoodsBrand.getUpdateTime() != null) {
            lqw.eq(GoodsBrand::getUpdateTime, iGoodsBrand.getUpdateTime());
        }
        return lqw;
    }
}




